#!/usr/bin/env python

import wx

from widget.statusBar import ESB_ALIGN_RIGHT, ESB_EXACT_FIT

class Gauge(object):
    '''Gauge class
    
    @author Julot <andy.julot@gmail.com>
    @since Friday, March 23, 2012. 10:31 AM'''

    def __init__(self, parent):
        self.parent = parent
    
    def create(self):
        status = self.parent
        status.SetFieldsCount(2)
        status.SetStatusWidths([-1, 150])
        self.all = _Gauge(status)
        status.AddWidget(self.all, ESB_ALIGN_RIGHT, pos=0)
        self.all.Hide()
        self.each = _Gauge(status)
        status.AddWidget(self.each, ESB_EXACT_FIT, pos=1)
    
    def destroy(self):
        self.all.Hide()
        self.each.Hide()
        #self.parent.SetFieldsCount(1)

class _Gauge(wx.Gauge):
    '''Gauge class
    
    @author Julot <andy.julot@gmail.com>
    @since Friday, April 06, 2012. 10:52 AM'''
    
    _range = 0
    _value = 0
    _oldPercent = 0
    
    @property
    def _percent(self):
        return int((self._value * 100.0) / self._range)
    
    def __init__(self, parent):
        super(_Gauge, self).__init__(parent, size=(150, -1))
        
    def update(self, total, progress):
        if total == 0:
            self.Pulse()
        else:
            if total and self._range != total: self.Range = total
            self._value = progress
            percent = self._percent
            if self._oldPercent != percent:
                self._oldPercent = percent
                self.SetValue(progress)
    
    def GetRange(self):
        return super(_Gauge, self).GetRange()
    
    def SetRange(self, value):
        self._range = value
        super(_Gauge, self).SetRange(value)
    
    Range = property(GetRange, SetRange)
    
    def GetValue(self):
        return super(_Gauge, self).GetValue()
    
    def SetValue(self, value):
        self._value = value
        super(_Gauge, self).SetValue(value)
        
    Value = property(GetValue, SetValue)
    